﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Diagnostics;

namespace WorkTimeRecorder
{
    public partial class MainWindow : Form
    {
        public MainWindow()
        {
            InitializeComponent();
            if (WindowState == FormWindowState.Minimized)
            {
                Hide();
                notify.Visible = true;
            }

        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“workDetailsDataSet.WorkDetails”中。您可以根据需要移动或删除它。
            this.workDetailsTableAdapter.Fill(this.workDetailsDataSet.WorkDetails);
            // TODO: 这行代码将数据加载到表“workDetailsDataSet.WorkDetails”中。您可以根据需要移动或删除它。
            this.workDetailsTableAdapter.Fill(this.workDetailsDataSet.WorkDetails);
            // TODO: 这行代码将数据加载到表“workDetailsDataSet.WorkDetails”中。您可以根据需要移动或删除它。
            workDetailsTableAdapter.Fill(workDetailsDataSet.WorkDetails);

        }

        

        private void Add_Click(object sender, EventArgs e)
        {

            string StrNum = NumText.Text;
            string StrPCnum = PCnumText.Text;
            string StrPhone = PhoneText.Text;
            string StrDeviceNum = DeviceNumText.Text;
            string StrContent = ContentText.Text;
            string StrTime = TimeText.Text;
            try
            {
                int IntTime = Convert.ToInt32(StrTime);
                OleDbConnection mycon = null;
                string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=WorkDetails.mdb;";
                mycon = new OleDbConnection(strcon);
                string sql = "INSERT INTO [WorkDetails] ([Num],[PCnum],[Phone],[DeviceNum],[Content],[Time]) VALUES ('" + StrNum + "','" + StrPCnum + "','" + StrPhone + "','" + StrDeviceNum + "','" + StrContent + "','" + IntTime + "'); ";
                OleDbCommand mycom = new OleDbCommand(sql, mycon);
                mycon.Open();
                mycom.ExecuteNonQuery();
                mycon.Close();
                workDetailsTableAdapter.Fill(workDetailsDataSet.WorkDetails);

                NumText.Text = null;
                PCnumText.Text = null;
                PhoneText.Text = null;
                DeviceNumText.Text = null;
                ContentText.Text = null;
                TimeText.Text = null;

            }
            catch
            {
                MessageBox.Show("请输入正确的工时，单位分钟");
            }
        }



        private void RefreshData_Click(object sender, EventArgs e)
        {
            workDetailsTableAdapter.Fill(workDetailsDataSet.WorkDetails);
        }



        private void Cal_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            OleDbConnection mycon = null;
            string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=WorkDetails.mdb;";
            mycon = new OleDbConnection(strcon);
            string sql = "SELECT [Time] FROM [WorkDetails];";
            OleDbDataAdapter mycom = new OleDbDataAdapter(sql, mycon);
            mycon.Open();
            mycom.Fill(ds, "Time");
            int sum = 0;
            int n = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                n = Convert.ToInt32(ds.Tables[0].Rows[i]["Time"].ToString());
                sum = sum + n;
            }
            Result.Text = Convert.ToString(sum);
            mycon.Close();
            workDetailsTableAdapter.Fill(workDetailsDataSet.WorkDetails);

        }


        private void TopMost_Click(object sender, EventArgs e)
        {
            if (TopMost != true)
            {
                TopMost = true;
                TopMostButton.Text = "已置顶";
            }
            else
            {
                TopMost = false;
                TopMostButton.Text = "未置顶";
            }
        }

        private void TimeText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Add_Click(sender, e);
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int CIndex = e.ColumnIndex;
                if (CIndex == 0)
                {

                    int _UID = Convert.ToInt32(dataGridView1[1, e.RowIndex].Value);

                    OleDbConnection mycon = null;
                    string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=WorkDetails.mdb;";
                    mycon = new OleDbConnection(strcon);
                    string sql = "DELETE * FROM [WorkDetails] WHERE [ID] =" + _UID + " ;";
                    OleDbCommand mycom = new OleDbCommand(sql, mycon);
                    mycon.Open();
                    mycom.ExecuteNonQuery();
                    mycon.Close();
                    workDetailsTableAdapter.Fill(workDetailsDataSet.WorkDetails);
                }
            }
            catch
            {
                MessageBox.Show("删除失败！");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process.Start("mailto:famio.liew@cppcb.com.tw?subject=BUG反馈-工时速记&content=请提供BUG的详细说明");
        }

        private void notify_Click(object sender, EventArgs e)
        {
            if (Visible == true)
            {
                Visible = false;
                ShowInTaskbar = false;
            }
            else
            {
                Visible = true;
                ShowInTaskbar = true;
                Activate();
            }
        }
        

        private void AddHR_Click(object sender, EventArgs e)
        {
            OleDbConnection mycon = null;
            string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=WorkDetails.mdb;";
            mycon = new OleDbConnection(strcon);
            string sql = "INSERT INTO [WorkDetails] ([Num],[PCnum],[Phone],[DeviceNum],[Content],[Time]) VALUES ('-----','-----','-----','-----','-----','0'); ";
            OleDbCommand mycom = new OleDbCommand(sql, mycon);
            mycon.Open();
            mycom.ExecuteNonQuery();
            mycon.Close();
            workDetailsTableAdapter.Fill(workDetailsDataSet.WorkDetails);
        }

        private void ReadMonth_Click(object sender, EventArgs e)
        {
            FilterDate.Items.Clear();
            DataSet ds = new DataSet();
            OleDbConnection mycon = null;
            string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=WorkDetails.mdb;";
            mycon = new OleDbConnection(strcon);
            string sql = "SELECT Mid(Format(WorkDate,'yyyy-mm-dd'),6,2) FROM WorkDetails GROUP BY Mid(Format(WorkDate, 'yyyy-mm-dd'), 6, 2); ";
            OleDbDataAdapter mycom = new OleDbDataAdapter(sql, mycon);
            mycon.Open();
            mycom.Fill(ds, "Month");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                FilterDate.Items.Add(ds.Tables[0].Rows[i]["Expr1000"]+"月份");
            }

            //FilterDate.DisplayMember = "Month";
            //FilterDate.ValueMember = "Month";
            //FilterDate.DataSource = ds.Tables[0];
            mycon.Close();
            workDetailsTableAdapter.Fill(workDetailsDataSet.WorkDetails);
        }
    }
}
